home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_009 / proff / lookup.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  168 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  * from K&R "The C Programming language"
  7.  * Table lookup routines
  8.  *
  9.  */
  10. #include <stdio.h>
  11. #include "proff.h"
  12. /*
  13.  * hash - for a hash value for string s
  14.  *
  15.  */
  16. hash(s)
  17. char *s;
  18. {
  19.     int    hashval;
  20.  
  21.     for (hashval = 0; *s != '\0';)
  22.         hashval += *s++;
  23.     return (hashval % HASHMAX);
  24. }
  25.  
  26. /*
  27.  * lookup - lookup for a string s in the hash table
  28.  *
  29.  */
  30. struct hashlist
  31. *lookup(s, hashtab)
  32. char *s;
  33. struct hashlist *hashtab[];
  34. {
  35.     struct hashlist *np;
  36.  
  37.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  38.         if (strcmp(s, np->name) == 0)
  39.             return(np);    /* found     */
  40.     return(NULL);        /* not found */
  41. }
  42.  
  43. /*
  44.  * install - install a string name in hashtable and its value def
  45.  * at a given hashtable.
  46.  */
  47. struct hashlist
  48. *install(name,def,hashtab)
  49. char *name;
  50. char *def;
  51. struct hashlist *hashtab[];
  52. {
  53.     int hashval;
  54.     struct hashlist *np, *lookup();
  55.     char *strsave(), *malloc();
  56.  
  57.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  58.         np = (struct hashlist *) malloc(sizeof(*np));
  59.         p_memoryus += sizeof(*np);
  60.         if (np == NULL)
  61.             return(NULL);
  62.         if ((np->name = strsave(name)) == NULL)
  63.             return(NULL);
  64.         hashval = hash(np->name);
  65.         np->next = hashtab[hashval];
  66.         hashtab[hashval] = np;
  67.     } else                    /* found..     */
  68.         free(np->def);            /* free prev.  */
  69.     if ((np->def = strsave(def)) == NULL)
  70.         return(NULL);
  71.     return(np);
  72. }
  73.  
  74. /*
  75.  * strsave - save string s somewhere
  76.  *
  77.  */
  78. char
  79. *strsave(s)
  80. char *s;
  81. {
  82.     char *p, *malloc();
  83.     register int n;
  84.  
  85.     n = strlen(s) + 1;
  86.     if ((p = malloc(n)) != NULL) {
  87.         p_memoryus += n;
  88.         strcpy(p, s);
  89.     }
  90.     return(p);
  91. }
  92.  
  93. /*
  94.  * lexinstal - instal a string name in hashtable and its value
  95.  *           used by lexical analyser to quickly match a token
  96.  *           and return its lexical value.
  97.  *
  98.  */
  99. struct lexlist
  100. *lexinstal(name,val,flag,lextable)
  101. char *name;
  102. int val;
  103. int flag;
  104. struct lexlist *lextable[];
  105. {
  106.     int hashval;
  107.     struct lexlist *np,*lexlook();
  108.     char *strsave(), *malloc();
  109.  
  110.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  111.         np = (struct lexlist *) malloc(sizeof(*np));
  112.         p_memoryus += sizeof(*np);
  113.         if (np == NULL)
  114.             return(NULL);
  115.         if ((np->name = strsave(name)) == NULL)
  116.             return(NULL);
  117.         hashval = hash(np->name);
  118.         np->link = lextable[hashval];
  119.         lextable[hashval] = np;
  120.     }
  121.     np->val = val;                /* replace prev */
  122.     np->flag = flag;
  123.     return(np);
  124. }
  125.  
  126. /*
  127.  * lexlook - lookup for a string s in the hash table
  128.  *         used by lexinstal only.
  129.  *
  130.  */
  131. struct lexlist
  132. *lexlook(s,table)
  133. char *s;
  134. struct lexlist *table[];
  135. {
  136.     struct lexlist *np;
  137.  
  138.     for (np = table[hash(s)]; np != NULL; np = np->link)
  139.         if (strcmp(s, np->name) == 0)
  140.             return(np);    /* found     */
  141.     return(NULL);        /* not found */
  142. }
  143.  
  144. /*
  145.  * remove an item from the hash table forever
  146.  *
  147.  */
  148. struct lexlist
  149. *remove(s, table)
  150. char *s;
  151. struct lexlist *table[];
  152. {
  153.     struct lexlist *np, *xp;
  154.  
  155.     np = table[hash(s)];
  156.     xp = np; 
  157.     while (np != NULL) {
  158.         if (strcmp(s, np->name) == 0) {
  159.             xp->link = np->link;    /* remove the link */
  160.             return(np);        /* return the lost */
  161.         }
  162.         xp = np; 
  163.         np = np->link;
  164.     }
  165.     return(NULL);
  166. }
  167.  
  168.